package com.itheima.proxy.dynamic.jdk;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * ProxyHandler:代理对象要做的事情
 * 代理对象要将逻辑业务和增强业务组织在一起
 */
public class ProxyHandler implements InvocationHandler {
    // 客户
    private Object client;

    /**
     * 接待具体的客户
     *
     * @param target
     */
    public ProxyHandler(Object target) {
        this.client = target;
    }

    public void start() {
        System.out.println("开启事务");
    }

    public void commit() {
        System.out.println("提交事务");
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        start();
        //目标方法执行
        Object result = method.invoke(client, args);
        commit();
        return result;
    }
}
